/************************************************************************************/
/*  Program name CAPFNDIMP_RESPONSIBILITY.sql                                       */
/*  Creater:  WONG Wing Kin (PCCW - UniHub)                                         */
/*************   History   *************************                                */
/* 30 NOV, 2005   WONG Wing Kin         First created                               */
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER FND_RESPONSIBILITY_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDIMP_RESPONSIBILITY';

	lv_Scc_Count	NUMBER			:=	0;
	lv_Err_Count	NUMBER			:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FND_RESPONSIBILITY;

	CURSOR Cur_G IS
	SELECT
		A.*,
		B.Responsibility_ID_N
	FROM
		CFND_RESPONSIBILITY			A,
		CRST_FND_RESPONSIBILITY_MAP	B
	WHERE	A.Responsibility_ID	=	B.Responsibility_ID;

	lv_Next_Seq_ID	NUMBER;

BEGIN

	--  RENAME
	UPDATE
		FND_RESPONSIBILITY
	SET
		Responsibility_Key	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(Responsibility_Key,1,4),Responsibility_Key)
	WHERE	Responsibility_ID	IN	(
								SELECT
									Responsibility_ID
								FROM
									CRST_FND_RESPONSIBILITY
								);

	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FND_RESPONSIBILITY_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT INTO CRST_FND_RESPONSIBILITY_MAP VALUES
			(
			C1.APPLICATION_ID,
			C1.RESPONSIBILITY_ID,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.RESPONSIBILITY_KEY,1,4),C1.RESPONSIBILITY_KEY),
			C1.APPLICATION_ID,
			lv_Next_Seq_ID,
			C1.RESPONSIBILITY_KEY
			);

			INSERT INTO FND_RESPONSIBILITY
			VALUES
			(
			C1.APPLICATION_ID,
			lv_Next_Seq_ID,													--RESPONSIBILITY_ID,
			C1.LAST_UPDATE_DATE,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_BY_NAME),				--LAST_UPDATE_DATE,
		--	C1.LAST_UPDATED_BY,
			C1.CREATION_DATE,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_BY_NAME),					--CREATED_BY,
			C1.LAST_UPDATE_LOGIN,
			C1.DATA_GROUP_APPLICATION_ID,
			C1.DATA_GROUP_ID,
			CAPBRIMP_MAPPING.GetFNDMenuIDMapping(C1.MENU_ID),				--MENU_ID,
			C1.TERM_SECURITY_ENABLED_FLAG,
			C1.START_DATE,
			C1.END_DATE,
			C1.GROUP_APPLICATION_ID,
			CAPBRIMP_MAPPING.GetFNDReqGrpIDMapping(C1.REQUEST_GROUP_ID),	--REQUEST_GROUP_ID,
			C1.VERSION,
			C1.WEB_HOST_NAME,
			C1.WEB_AGENT_NAME,
			C1.RESPONSIBILITY_KEY
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	BEGIN
		FOR C2 IN Cur_G LOOP
			INSERT INTO CFND_RESPONSIBILITY VALUES
			(
				C2.APPLICATION_ID,
				C2.RESPONSIBILITY_ID_N,
				CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
				CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
			);
		END LOOP;
	EXCEPTION
		WHEN OTHERS THEN
			DBMS_OUTPUT.Put_Line(SQLERRM);
			lv_Err_Count	:=	lv_Err_Count + 1;
	END;

	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/

ALTER TRIGGER FND_RESPONSIBILITY_CT1 ENABLE;
